﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler
{
    public class FistRoleContainCommandHandler : IRequestHandler<FistRoleContainCommand, ApiResult<string>>
    {
        IBaseReponsitory<App> Apps;

        public FistRoleContainCommandHandler(IBaseReponsitory<App> Apps)
        {
            this.Apps = Apps;
        }

        public async Task<ApiResult<string>> Handle(FistRoleContainCommand request, CancellationToken cancellationToken)
        {
            var data = request.RoleId.ToString();

            var app = await Apps.GetQuery().Where(c => c.Group_Id == request.GroupId).ToListAsync();//选择
            string AppArrId = "";
            if (app.Count() > 0)
            {
                foreach (var item in app)
                {
                    if (item.Role_ArrId.Contains(data))
                    {
                        //把用户item.App_Id合成数组放入AppArrId
                        AppArrId += item.App_Id.ToString() + ",";
                    }
                }
                AppArrId = AppArrId.Substring(0, AppArrId.Length - 1); //最后把AppArrId替换最后一个逗号为空
                return new ApiResult<string>
                {
                    Code = ApiEnum.Success,
                    Message = "获取成功",
                    Data = AppArrId
                };
            }
            else 
            {
                return new ApiResult<string>
                {
                    Code = ApiEnum.False,
                    Message = "没有分配部门",
                };
            }
        }
    }
}
